# Create your views here.
from django.http import HttpResponse
from django.template import loader, Context, RequestContext

import datetime

from .models import Student, Teacher
from django.shortcuts import render_to_response

class Person(object):
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex


    def say(self):
        return 'say mayname'

def home(request):
    t = loader.get_template('index.html')
    user = {'name':'geo', 'age': 30, 'sexual': 'male'}
    
    person = Person('geo2', '29', 'Femal')

    book_list = ['python', 'ruby', 'java']
    
    c = Context({'name': 'Django-test',
                 'user': user,
                 'book_list': book_list,
                 'book_list2': [],
                 'today': datetime.datetime.now()})     # data to template

    #return HttpResponse('<h1>Hello World</h1>')
    return HttpResponse(t.render(c))


def time(request):
    
    t = loader.get_template('time.html')
    id = request.GET.get('id')
    name = request.GET.get('name')
    c = Context({'today': datetime.datetime.now(), 
                 'id': id,
                 'name': name})
    return HttpResponse(t.render(c))


def foo(request, p1, p2):
    
    t = loader.get_template('time.html')
    c = Context({'today': datetime.datetime.now(), 
                 'id': p1,
                 'name': p2})
    return HttpResponse(t.render(c))


def bar(request, id, name):
    """
    r'^bar/(?P<id>\d{4})/(?P<name>\w+)/$' # and the ip, name must match in the url config
    
    under this, the request.GET.get() will get nothing.
    """
    t = loader.get_template('time.html')
    c = Context({'today': datetime.datetime.now(), 
                 'id': id,
                 'name': name})
    return HttpResponse(t.render(c))
    
    
def before_info(view_func):
    """
    do something before every view function
    """
    def new_view_func(*args, **kwargs):
        print 'just for test'
        return view_func(*args, **kwargs)
    return new_view_func
    
    
@before_info
def students(request):
    t = loader.get_template('time.html')
    students = Student.objects.all().filter(age__gte = 28).order_by('-age') 
    students = Student.objects.all()
    teacher = Teacher.objects.get(id =2) 
    students = teacher.student_teacher.all().order_by('-age')
    c = Context({'students': students,})
    return HttpResponse(t.render(c))
    

def proc(request):   
    return {'message': 'I am here to test'}
   
def view1(request): 
    t = loader.get_template('view.html')
    print t
    students = {'name': 'zhangsan',
                'message': 'I am here to test'}  # message is public, we use pocessor to deal it
    c = Context({'students': students,})
    print c
    return HttpResponse(t.render(c))


def view2(request):
    """
    use processor to get common message to students
    show how to use RequestContext
    """
    return render_to_response('view.html', 
                              {'sex': 'male'},
                              context_instance = RequestContext(request,
                                                               {'name':'zhangsan'},
                                                               processors = [proc])
                              )
    
    
    
    
    
    
    
    
